package org.teststack.Test123;


import com.jayway.restassured.RestAssured;
import com.jayway.restassured.response.Response;

import java.util.HashMap;
import java.util.Map;

public class TestSuitt {

    public static void main(String[] args) {
        String zyOpUser = loginAndGetCookie();
        performAuthenticatedRequest(zyOpUser);
    }
    public static String loginAndGetCookie() {
        Map<String, String> formParams = new HashMap<>();
        formParams.put("method", "method");
        formParams.put("random", "123");
        formParams.put("loginName", "liutao");
        formParams.put("psd", "ISGJ0ur/yn/5jIZbm3jVYM3TkzYTidyjTzTG5YW5lxe17Om4cUEMJjNCx96Tuciw8TYvwXZENQv+xWmrN6bdJBsdE3+Hc+7aw/aJNFnBCVCKsLPWvJdFxa+xMfgWZwuEgUgGrq3X1funSUr0RpY42nn1MAUQVy0S3I3Yg96OBmNyPno+G/pkwA1yBMnAptKbzQtGO6hnGzKCAYXg2kdi3RoEI3nka0H8z0k+0CDritt7nx2EbVyc+GijZmub6uiMyq0tHOLX9jPy2tm/PJq9bsf01E7RMkuyg3r+C8uG5FHUhNgU/zo/02q6lLYBY/1ZR43lHcPnh1/0og+WdbWcIQ==");

        Response response = RestAssured.given()
                .baseUri("http://10.1.10.141:9031")
                .when()
                .header("Referer", "http://10.1.10.141:9031/login.do?method=enterDddl&PARAFLG=1&serviceCode=entrance_unite\n")
                .formParams(formParams)
                .post("/login.do?method=login_qyzh");

            return response.getCookie("zyOpUser");
        }

    public static void performAuthenticatedRequest(String cookie) {
            Response response = RestAssured.given()
                    .header("Cookie","zyOpUser=" + cookie)

                    .get("http://10.1.10.141:9031/unite.do?method=enterGrzx&csrftoken="); // 注意：可能需要有效的 CSRF Token

            int statusCode = response.getStatusCode();
            String body = response.getBody().asString();

//        String htmlContent = ";

        // 使用Jsoup解析HTML字符串
//        Document doc = Jsoup.parse(htmlContent);
////        if (!errorDivs.isEmpty()) {
////            // 如果找到了，可以进一步处理，比如打印错误信息
////            System.out.println("找到了错误消息: " + errorDivs.text());
////        } else {
////            // 如果没有找到，可以执行其他操作
////            System.out.println("没有找到错误消息");
//            // 使用选择器查找具有'error'类的div元素
//        Elements errorDivs = (Elements) doc.select("div.error");

        if (statusCode == 401) {
                // 处理 Cookie 过期的情况
                String newCookie = loginAndGetCookie();
                performAuthenticatedRequest(newCookie); // 递归调用以使用新 Cookie 重新尝试
            } else {
                // 正常处理响应
                System.out.println("Status Code: " + statusCode);
                System.out.println("Response Body: " + body);
                // 在这里添加更多的断言或日志
//            }
               System.out.println("Status Code: " + statusCode);
               System.out.println("Response Body: " + body);
        }


    }
}

